
(in-package #:smugmug)

(defmethod get-albums ((session session) &optional nickname site-password)
  (let ((parameters `(("Heavy" . "true"))))
    (when nickname (push `("NickName" . ,nickname) parameters))
    (when site-password (push `("SitePassword" . ,site-password) parameters))
    (let ((response (rest-request :session session
                                  :method "smugmug.albums.get"
                                  :parameters parameters)))
      (let ((albums '()))
        (dom:map-node-list #'(lambda (album-node)
                               (push (make-instance 'album
                                                    :album-id (dom:get-attribute album-node "id")
                                                    :title (dom:get-attribute album-node "Title")
                                                    :category (elt (dom:get-elements-by-tag-name album-node "Category") 0)
                                                    :session session)
                                     albums))
                           (dom:get-elements-by-tag-name response "Album"))
        (reverse albums)))))

;;; TODO implement smugmug.albums.getInfo

(defmethod create-album ((category category) title)
  (let ((response (rest-request :session (session category)
                                :method "smugmug.albums.create"
                                :parameters `(("Title" . ,title)
                                              ("CategoryID" . ,(category-id category))))))
    (make-instance 'album
                   :album-id (dom:get-attribute (elt (dom:get-elements-by-tag-name response "Album") 0) "id")
                   :title title
                   :category category
                   :session (session category))))

;;; TODO implement smugmug.albums.changeSettings

(defmethod sort-album ((album album) field &optional (order :ascending))
  (let ((field (ecase field
                 (:filename "FileName")
                 (:caption "Caption")
                 (:time "DateTime")))
        (order (ecase order
                 (:ascending "ASC")
                 (:descending "DESC"))))
    (rest-request :session (session album)
                  :method "smugmug.albums.reSort"
                  :parameters `(("AlbumID" . ,(album-id album))
                                ("By" . ,field)
                                ("Direction" . ,order)))))

(defmethod delete-album ((album album))
  (rest-request :session (session album)
                :method "smugmug.albums.delete"
                :parameters `(("AlbumID" . ,(album-id album)))))

;;; TODO implement smugmug.albums.getStats

(defmethod get-album-stats ((album album) year month)
  (let ((response (rest-request :session (session album)
                                :method "smugmug.albums.getStats"
                                :parameters `(("AlbumID" . ,(album-id album))
                                              ("Month" . ,month)
                                              ("Year" . ,year)
                                              ("Heavy" . "false")))))
    (make-instance 'stats
                   :album-id (dom:get-attribute (elt (dom:get-elements-by-tag-name response "Album") 0) "id")
                   :title title
                   :category category
                   :session (session category))))

(defmethod print-object ((album album) stream)
  (print-unreadable-object (album stream :type t :identity :t)
    (format stream "~A ~S"
            (album-id album)
            (title album))))
